package com.ebelter.btcomlib.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ZCQ on 2017/11/27
 * 倒计时按钮
 */

@SuppressLint("AppCompatCustomView")
public class CountDownTextView extends TextView {


    public static final String LAST_SEND_CODE_TIME = "lastSendTime";
    private String primalTitle;
    private MyHandle myHandle;

    public CountDownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    /**
     * 开始执行计时的类，可以在每秒实行间隔任务
     */
    private Timer timer;
    /**
     * 每秒时间到了之后所执行的任务
     */
    private TimerTask timerTask;

    private void init() {

        setGravity(Gravity.CENTER);
        myHandle=new MyHandle(this,Looper.getMainLooper());
       // startWord();

        primalTitle=getText().toString().trim();
        LogUtils.i("CountDownButton","原始的title : "+primalTitle);

    }


    private class MyHandle extends Handler{
        private WeakReference<View> weakReference;

        MyHandle(View view, Looper looper) {
            super(looper);
            weakReference=new WeakReference<>(view);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(weakReference.get()!=null){

                if (msg.what==1){
                    setEnabled(true);
                    setText(primalTitle);
                    //setTextColor(Color.parseColor("#1994ee"));
                    stopWord();
                    removeCallbacksAndMessages(null);
                }else if (msg.what==2){
                    setEnabled(false);
                    setText(msg.arg1 +" S");
                    // setTextColor(Color.parseColor("#999999"));
                }
            }else {
                removeCallbacksAndMessages(null);
            }
        }
    }

    private int countSecond = 120;//默认120秒

    public void setCountSecond(int second){//单位 秒
        this.countSecond=second;
    }


    public void click(){

        SpUtil.writeLong(LAST_SEND_CODE_TIME,System.currentTimeMillis());
        startWord();
    }

    public void startWord() {
        stopWord();
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (myHandle!=null){
                    long lastSendTime = SpUtil.readLong(LAST_SEND_CODE_TIME);
                    long currentTime = System.currentTimeMillis();
                    if (currentTime-lastSendTime>=countSecond*1000){
                        myHandle.sendEmptyMessage(1);//可用
                    }else {
                        Message message=myHandle.obtainMessage();
                        message.what=2;
                        //setText((countSecond-(currentTime-lastSendTime)/1000) +" S");
                        message.arg1= (int) (countSecond-(currentTime-lastSendTime)/1000);
                        myHandle.sendMessage(message);
                    }

                }
            }
        };
        timer.schedule(timerTask,0,1000);
    }
    private void stopWord(){
        if (timerTask!=null){
            timerTask.cancel();
            timerTask=null;
        }
        if (timer!=null){
            timer.cancel();
            timer=null;
        }
    }

    public void exitActivity(){
        stopWord();
        if(myHandle!=null){
            myHandle.removeCallbacksAndMessages(null);
            myHandle=null;
        }
    }

}
